package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.UserLogin;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbFile;
import com.example.ymm.modules1.entity.my.user.DbIntegralLog;
import com.example.ymm.modules1.mapper.my.user.DbFileMapper;
import com.example.ymm.modules1.service.dto.DbFileQueryCriteria;
import com.example.ymm.modules1.service.user.DbFileService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

@Service
@Slf4j
public class DbFileServiceImpl extends BaseServiceImpl<DbFileMapper, DbFile> implements DbFileService {


    @Value("${sys.localPath}")
    String localPath;

    @Override
    public TableList<DbFile> list(DbFileQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbFile> page = new Page<DbFile>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbIntegralLog.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<DbFile> tableList=new TableList(page);
        return tableList;
    }

    public List<DbFile> queryAll(DbFileQueryCriteria criteria) {
        List<DbFile> result =baseMapper.selectList(QueryHelpPlus.getPredicate(DbFile.class, criteria));
        return result;
    }


    @Override
    public DbFile addFn(DbFile file) {

        String userId = getUserId();
        String userName = getUserName();
        file.setUserId(userId);
        file.setUserName(userName);
        saveOrUpdate(file);
        return file;
    }

    @Override
    public void deleteFn(String fileId) {
        DbFile dbFile = getById(fileId);
        if(UtilValidate.isEmpty(dbFile)){
            throw new YmException("参数有误");
        }
        removeById(dbFile);//
        String path = dbFile.getImgUrl().replaceAll("http://220.161.77.66:10111", localPath);
        File file = new File(path);
        if(file.exists()){
            file.delete();
        }
    }
}
